package com.kongge.formlike;

import android.text.TextUtils;
import android.view.View;

import com.kongge.formlike.bean.FormBean;
import com.kongge.formlike.bean.ViewBean;
import com.kongge.formlike.service.ViewParser;
import com.kongge.formlike.service.check.CheckView;
import com.kongge.formlike.service.check.ICheckItem;
import com.kongge.formlike.service.check.OnCheckItemClickListener;
import com.kongge.formlike.service.rule.IRule;
import com.kongge.formlike.service.rule.RuleFactory;
import com.kongge.formlike.util.ProtocolContants;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * author:kongge
 * date:2018/7/20
 * layout:
 * description:
 */

public class Form {

    private FormBean formBean;

    private static Class defaultCheckView = CheckView.class;

    private OnFormCommitListener formCommitListener;

    public Form() {
    }

    public void parseView(View rootView, String configJson) {
        ViewParser viewParser = new ViewParser(defaultCheckView);
        formBean = viewParser.parseView(rootView, configJson);
        if (formBean == null || !formBean.isDataRight()) {
            return;
        }
        List<ViewBean> viewBeanList = formBean.getViewBeanList();
        for (ViewBean viewBean : viewBeanList) {
            if (TextUtils.equals(viewBean.getType(), ProtocolContants.STR_SUBMIT)) {
                ICheckItem checkItem = viewBean.getCheckItem();
                checkItem.setOnCheckItemClickListener(new OnCheckItemClickListener() {
                    @Override
                    public void onCheckItemClick(ICheckItem checkItem) {
                        commit();
                    }
                });
            }
        }
    }

    public void commit() {
        if (formCommitListener == null) {
            return;
        }
        // 没有错误信息
        ICheckItem errorCheckItem = null;
        IRule errorRule = null;
        List<ViewBean> viewBeanList = formBean.getViewBeanList();
        for (ViewBean viewBean : viewBeanList) {
            if (!TextUtils.equals(viewBean.getType(), ProtocolContants.STR_SUBMIT)) {
                ICheckItem checkItem = viewBean.getCheckItem();
                List<IRule> ruleList = viewBean.getRuleList();
                if (ruleList != null && ruleList.size() > 0) {
                    for (IRule rule : ruleList) {
                        if (!rule.check(checkItem)) {
                            errorCheckItem = checkItem;
                            errorRule = rule;
                            break;
                        }
                    }
                    if (errorRule != null) {
                        break;
                    }
                }
            }
        }
        if (errorCheckItem == null) {
            Map<String, String> paramMap = getParamMap(formBean);
            formCommitListener.onFormCommitChecked(this, formBean.getConfigMap(), paramMap);
        } else {
            formCommitListener.onFormCommitError(this, errorCheckItem, errorRule);
        }
    }

    private Map<String, String> getParamMap(FormBean formBean) {
        Map<String, String> paramMap = new LinkedHashMap<>();
        List<ViewBean> viewBeanList = formBean.getViewBeanList();
        for (ViewBean viewBean : viewBeanList) {
            String paramName = viewBean.getParamName();
            ICheckItem checkItem = viewBean.getCheckItem();
            if (!TextUtils.isEmpty(paramName) && checkItem != null) {
                paramMap.put(paramName, checkItem.getCheckContent());
            }
        }
        return paramMap;
    }

    public void setFormCommitListener(OnFormCommitListener formCommitListener) {
        this.formCommitListener = formCommitListener;
    }

    /**
     * 添加规则
     * @param ruleName 规则名称
     * @param c 规则检查类，需要实现IRule接口，或者继承AbsRule
     */
    public static void addRule(String ruleName, Class c) {
        RuleFactory.addRule(ruleName, c);
    }

    /**
     * 设置ICheckItem
     * @param c 需要实现ICheckItem接口,或者集成CheckView
     */
    public static void setCheckView(Class c) {
        defaultCheckView = c;
    }
}
